package com.sky.controller.admin;

import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.SetMealService;
import com.sky.vo.SetmealVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/admin/setmeal")
@Api(tags = "套餐管理")
@Slf4j
public class SetMealController {
    @Autowired
    private SetMealService setMealService;

    /**
     * 新增套餐
     * @param setmealDTO
     * @return
     */
    @ApiOperation("新增套餐")
    @PostMapping
//    精确清除套餐缓存
    @CacheEvict(cacheNames = "SetmealCache",key = "#setmealDTO.categoryId")
    public Result saveSetMeal(@RequestBody SetmealDTO setmealDTO){
        log.info("接收到的套餐参数为:{}",setmealDTO);
        setMealService.saveSetMeal(setmealDTO);
        return Result.success();
    }

    /**
     * 套餐分页查询
     * @param setmealPageQueryDTO
     * @return
     */
    @ApiOperation("套餐分页查询")
    @GetMapping("/page")
    public Result pageSetmeal(SetmealPageQueryDTO setmealPageQueryDTO){
        PageResult pageResult=setMealService.pageSetmeal(setmealPageQueryDTO);
        log.info("套餐分页查询参数为:{}",setmealPageQueryDTO);
        return Result.success(pageResult);
    }
    /**
     * 批量删除套餐
     */
    @ApiOperation("批量删除套餐")
    @DeleteMapping
//    清除SetmealCache下的所有缓存
    @CacheEvict(cacheNames = "SetmealCache",allEntries = true)
    public Result deleteSetmealByIds(@RequestParam List<Long> ids){
        setMealService.deleteSetmealByIds(ids);
        return Result.success();

    }
    /**
     * 根据id查询套餐
     */
    @ApiOperation("根据id查询套餐")
    @GetMapping("/{id}")
    public Result getById(@PathVariable Long id){
        log.info("要查询的套餐的id为:{}",id);
        SetmealVO setmealVO=setMealService.getById(id);
        return Result.success(setmealVO);
    }

    /**
     * 修改套餐
     */
    @ApiOperation("修改套餐")
    @PutMapping
    @CacheEvict(cacheNames = "SetmealCache",allEntries = true)
    public Result UpDateSetmeal(@RequestBody SetmealDTO setmealDTO) {
        setMealService.UpDateSetmeal(setmealDTO);
        return Result.success();
    }

    /**
     * 起售停售套餐
     */
    @ApiOperation("起售停售套餐")
    @PostMapping("/status/{status}")
    @CacheEvict(cacheNames = "SetmealCache",allEntries = true)
    public Result StatusSetmeal (@PathVariable Integer status,Long id){
        log.info("起售/停售的套餐id为:{},想要执行的操作:{}",id,status);
        setMealService.StatusSetmeal(status,id);
        return Result.success();
    }
}
